package service.impl;

import bean.Goods;
import bean.Cart;
import bean.CartItem;
import dao.impl.CartItemImpl;
import service.CartService;

import java.util.List;

public class CartServiceImpl implements CartService {

    private CartItemImpl cartItem = new CartItemImpl();

    @Override
    public int addbook(Goods goods, int userId) {

        CartItem item = new CartItem(null, goods.getId(), userId, goods.getName(), 1, goods.getPrice(), goods.getPrice());

        return cartItem.addbook(item);
    }

    @Override
    public int updateCount(int itemId, int count) {
        return cartItem.updateCount(itemId,count);
    }

    @Override
    public int deletebook(int itemId, int userId) {

        return cartItem.deletebook(itemId,userId);
    }

    @Override
    public int clear(int userId) {
        return cartItem.clear(userId);
    }

    @Override
    public Cart getCartInfo(int userId) {

        List<CartItem> list = cartItem.query(userId);

        if(list.size() == 0){
            return null;
        }

        Cart cart = new Cart();
        cart.setItems(list);
        return cart;
    }

    @Override
    public int syncInfo(Goods goods) {
        return cartItem.syncInfo(goods);
    }
}
